import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

export function getToken() {
  return sessionStorage.getItem(TokenKey) || Cookies.get(TokenKey)
}

export function setToken(token) {
  sessionStorage.setItem(TokenKey, token)
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  sessionStorage.removeItem('appId')
  sessionStorage.removeItem('appName')
  localStorage.removeItem('userInfo')
  sessionStorage.removeItem(TokenKey)
  return Cookies.remove(TokenKey)
}

export function setAppId(appId) {
  return sessionStorage.setItem('appId', appId)
}

export function getAppId() {
  return sessionStorage.getItem('appId')
}

export function setAppName(appName) {
  return sessionStorage.setItem('appName', appName)
}

export function getAppName() {
  return sessionStorage.getItem('appName')
}

export function setUserInfo(userInfo) {
  localStorage.setItem('userInfo', userInfo)
}

export function getUserInfo() {
  try {
    return JSON.parse(localStorage.getItem('userInfo')) || {}
  } catch (e) {
    return {}
  }
}

export function setPreview() {
  return sessionStorage.setItem('isPreview', 1)
}

export function removePreview() {
  sessionStorage.removeItem('isPreview')
}

export function isPreview() {
  return !!sessionStorage.getItem('isPreview')
}